package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Reserve;
import com.situ.gym.pojo.query.ReserveQuery;
import com.situ.gym.pojo.vo.ReserveVO;
import com.situ.gym.service.IReserveService;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Guan
 * @since 2025-08-28
 */
@RestController
@RequestMapping("/reserve")
public class ReserveController {
    @Autowired
    private IReserveService reserveService;

    //查找所有预约
    @GetMapping("/selectAll")
    public Result<List<Reserve>> selectAll() {
        List<Reserve> list = reserveService.list();//mybatisplus提供的查找所有方法
        return Result.ok(list);
    }

    @GetMapping("/list")
    //分页查找
    //Result:是自定义返回类型状态码code、消息msg 和 数据data等字段
    public Result<ReserveVO> list(ReserveQuery reserveQuery) {//Restful风格，只能发get
        System.out.println("ReserveController.list");
        IPage<ReserveVO> page = reserveService.list(reserveQuery);//自己写的分页查找方法
        return Result.ok(page);
    }

    @GetMapping("/listByUserId")
    //按userId分页查找,用于用户登录后查看自己预约的课程
    public Result<ReserveVO> listByUserId(ReserveQuery reserveQuery) {//Restful风格，只能发get
        System.out.println("ReserveController.listByUserId");
        IPage<ReserveVO> page = reserveService.listByUserId(reserveQuery);//自己写的分页查找方法
        return Result.ok(page);
    }

    //按id删除
    // /reserve/deleteById?id=1
    // /reserve/deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        reserveService.removeById(id);
        return Result.ok("删除成功");
    }

    //批量删除
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {//Variable变量
        //使用mp
        //reserveService.removeBatchByIds(Arrays.asList(ids));

        //自己写
        reserveService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回前台
    //添加
    /*@PostMapping("/add")
    public Result add(@RequestBody Reserve reserve) {
        System.out.println(reserve);
        //简单的增删改查使用mp,不用自己写
        reserveService.save(reserve);
        return Result.ok("添加成功");
    }*/

    @PostMapping("/add")
    public Result<Void> add(@RequestBody Reserve reserve) {
        // 校验是否已预约
        if (reserveService.lambdaQuery()
                .eq(Reserve::getUserId, reserve.getUserId())
                .eq(Reserve::getCourseId, reserve.getCourseId())
                .count() > 0) {
            return Result.error("您已预约该课程，不可重复预约！");
        }
        System.out.println(reserve);
        //简单的增删改查使用mp,不用自己写
        reserveService.save(reserve);
        return Result.ok("添加成功");
    }

    //更新
    //更新前回显数据
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Reserve reserve = reserveService.getById(id);//根据前台传来的参数id查询数据库，返回对应的 Reserve 对象
        return Result.ok(reserve);
    }

    //执行更新
    @PutMapping("/update")
    public Result update(@RequestBody Reserve reserve) {
        System.out.println(reserve);
        //使用mp
        reserveService.updateById(reserve);//有选择的更新，前台传来值的属性（列）就更新，没有值保持原来的
        //reserveService.saveOrUpdate(reserve);//添加更新放一起的写法
        return Result.ok("更新成功");
    }

    //状态更新
    @PutMapping("/updateStatus/{id}/{status}")
    private Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Reserve reserve = new Reserve();
        reserve.setId(id);
        reserve.setStatus(status);
        reserveService.updateById(reserve);//有选择的更新，前台传来值的属性（列）就更新，没有值保持原来的
        return Result.ok("更新成功");
    }
}

